import Vue from 'vue'
import Vuex from './vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    counter: 0,
    isLoading: false
  },
  mutations: {
    SET_ADD(state) {
      state.counter++
    },
    SET_IS_LOADING(state, flag){
      state.isLoading = flag
    }
  },
  actions: {
    add({ commit }) {
      commit('SET_IS_LOADING', true)
      setTimeout(() => {
        commit('SET_ADD')
        commit('SET_IS_LOADING', false)
      }, 1000);
    }
  },
  getters: {
    doubleCounter(state) { // 计算剩余数量
      return state.counter * 2;
    }
  },
  modules: {
  }
})
